<template>
  <div class="basic-layout">
    <!-- 顶部导航栏 -->
    <header class="header">
      <div class="header-left">
        <router-link to="/" class="logo">
          <img src="@/assets/logo.webp" alt="logo">
          <span class="title">餐饮预订系统</span>
        </router-link>
      </div>

      <div class="header-right">
        <template v-if="userInfo">
          <!-- 已登录显示用户信息和退出 -->
          <el-dropdown @command="handleCommand">
            <span class="user-info">
              <el-avatar :size="32" :src="userInfo.avatar">
                {{ userInfo.nickname?.charAt(0) }}
              </el-avatar>
              <span class="nickname">{{ userInfo.nickname }}</span>
            </span>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item command="profile">个人中心</el-dropdown-item>
                <el-dropdown-item command="orders">我的订单</el-dropdown-item>
                <el-dropdown-item divided command="logout">退出登录</el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </template>
        <template v-else>
          <!-- 未登录显示登录注册按钮 -->
          <el-button type="text" @click="$router.push('/login')">登录</el-button>
          <el-button type="primary" @click="$router.push('/register')">注册</el-button>
        </template>
      </div>
    </header>

    <!-- 主要内容区域 -->
    <main class="main">
      <router-view></router-view>
    </main>

    <!-- 底部版权信息 -->
    <footer class="footer">
      <p>Copyright © 2025 餐饮预订系统 All Rights Reserved.</p>
    </footer>
  </div>
</template>

<script setup>
import { computed } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessageBox } from 'element-plus'
import { useUserStore } from '@/stores/user'
import { getUserInfo } from '@/utils/auth'
import router from '@/router'


// const router = useRouter()
const userStore = useUserStore()

// 获取用户信息
const userInfo = computed(() => getUserInfo())

// 处理下拉菜单命令
const handleCommand = (command) => {
  switch (command) {
    case 'profile':
      router.push('/user/profile')
      break
    case 'orders':
      router.push('/user/orders')
      break
    case 'logout':
      handleLogout()
      break
  }
}

// 处理退出登录
const handleLogout = () => {
  ElMessageBox.confirm('确定要退出登录吗?', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    userStore.logout()
    router.push('/login')
  })
}
</script>

<style scoped>
.basic-layout {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

.header {
  height: 60px;
  padding: 0 20px;
  background: #fff;
  box-shadow: 0 2px 8px rgba(0,0,0,.1);
  display: flex;
  align-items: center;
  justify-content: space-between;
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 1000;
}

.header-left {
  .logo {
    display: flex;
    align-items: center;
    text-decoration: none;

    img {
      height: 32px;
      margin-right: 10px;
    }

    .title {
      font-size: 20px;
      color: #333;
      font-weight: bold;
    }
  }
}

.header-right {
  display: flex;
  align-items: center;
  gap: 20px;

  .user-info {
    display: flex;
    align-items: center;
    cursor: pointer;

    .nickname {
      margin-left: 8px;
      color: #333;
    }
  }
}

.main {
  flex: 1;
  margin-top: 60px;
  padding: 20px;
  background: #f5f5f5;
}

.footer {
  padding: 20px;
  text-align: center;
  background: #fff;
  color: #666;
}
</style>
